package com.hp.es.elasticsearch.controller;

import com.hp.es.elasticsearch.service.IElasticService;
import com.hp.es.elasticsearch.util.CommonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Random;

/**
 * @Author hp
 * @Title 测试controller
 * @Package
 * @Date 2021/10/18 16:57
 * @Description
 * @Version 1.0
 */
@Slf4j
@RestController
@RequestMapping(value = "/test")
public class ElasticController {
    @Autowired
    private IElasticService elasticService;

    @GetMapping(value = "performanceTest")
    public String performanceTest(@RequestParam(value="name", defaultValue="") String name) {
        log.info("进入测试，参数name的值为:{}", name);
        if (CommonUtil.isEmpty(name)) {
            return "name cannot be null";
        } else {
            String str = getRandomString(16);
            log.info("进入测试，返回值为:{}", str);
            return str;
        }
    }

    //length用户要求产生字符串的长度
    public static String getRandomString(int length){
        String str="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        Random random=new Random();
        StringBuffer sb=new StringBuffer();
        for(int i=0;i<length;i++){
            int number=random.nextInt(62);
            sb.append(str.charAt(number));
        }
        return sb.toString();
    }
}
